package com.claa.springboot.components;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 登陆拦截器
 * @author 0283000121
 *
 */
public class LoginInterceptor implements HandlerInterceptor {

	/**
	 * 请求访问前检查，查看是否登陆，已登录放行，否则返回登陆页
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		Object user = request.getSession().getAttribute("LoginUser");
		if (user == null) {
			// 没有登陆
			request.setAttribute("error", "没有权限，请先登陆");
			request.getRequestDispatcher("/index.html").forward(request, response);
			return false;
		}
		else {
			// 已经登陆
			return true;
		}		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}
